/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package examplesocket;

import java.io.IOException;
import java.net.Socket;
import java.security.spec.X509EncodedKeySpec;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author 5m
 */
public class Stream
{  
    private User currentUser;
    private ServerReaderStream mySr;
    private ServerWriterStream mySw;
    private ServerWriterBytes mySwB;
    private ServerReaderBytes mySrB;
    private Socket s;
    Stream(Socket s,User me)
    {
        mySr = new ServerReaderStream(s, this);
        mySw = new ServerWriterStream(s);
        mySrB = new ServerReaderBytes(s, this);
        mySwB = new ServerWriterBytes(s);
        currentUser = me;
        this.s=s;
    }

    boolean isConnected()
    {
        return true;
    }
    
    void startStream()
    {
        mySwB.start();
        mySrB.start();
        mySwB.send(CryptHelper.getInstance().getPK().getEncoded());
        mySw.start();
    }
    
    void stopStream()
    {
        mySr.serviceStop();
        try {
            s.close();
        } catch (IOException ex) {
            System.err.println("Unable to stop socket in stream");
        }
    }

    User getCurrentUser()
    {
        return currentUser;
    }
    
    void send(String message)
    {
        mySw.send(message);
    }
    
    Socket getSocket()
    {
        return s;
    }
    
    void changeStreamReader()
    {
        mySr.start();
        mySrB.serviceStop();
    }
}
